class EventEmitter {
    constructor() {
        this._events = []
    }
    on = (evt, fn) => {
        if (typeof fn !== 'function') {
            throw new TypeError(`TypeErr: not a funciton`)
        }
        (this._events[evt] || (this._events[evt] = [])).push(fn);
    }
    emit = (evt, ...args) => {
        if (!this._events[evt]) return false
        const fns = [...this._events[evt]]
        fns.forEach(fn => {
            fn.apply(this, args)
        })
        return true
    }
    once(evt, fn) {
        const offFn = () => {
            fn.apply(this)
            this.off(evt, offFn)
        }
        this.on(evt, offFn)
    }
    off(evt, fn) {
        if (!this._events[evt]) return
        if (!fn) {
            this._events[evt] && (this._events[evt].length = 0)
        } else {
            const fns = this._events[evt]
            for (let i = 0; i < fns.length; i++) {
                if (fn === fns[i] || fn === fns[i].fn) {
                    fns.splice(i, 1)
                    break
                }
            }
        }
    }
    removeAllLisenter(evt) {
        if (evt) {
            this._events[evt] && (this._events[evt].length = 0)
        } else {
            this._events = []
        }
    }
}
export const eventEmit = new EventEmitter()